import {
    Component,
    Injector,
    ViewChild,
    ViewEncapsulation
} from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { AppConsts } from '@shared/AppConsts';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import { HttpClient } from '@angular/common/http';
import {
    EntityDtoOfInt64,
    SupplierListDto,
    SupplierServiceProxy
} from '@shared/service-proxies/service-proxies';
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';
import { Paginator } from 'primeng/components/paginator/paginator';
import { Table } from 'primeng/components/table/table';
import { CreateOrEditSupplierComponent } from './create-or-edit-supplier/create-or-edit-supplier.component';
import { finalize } from 'rxjs/operators';

@Component({
    selector: 'app-supplier',
    templateUrl: './supplier.component.html',
    styleUrls: ['./supplier.component.css']
})
export class SupplierComponent extends AppComponentBase {
    @ViewChild('createOrEditSupplierModal') createOrEditSupplierModal: CreateOrEditSupplierComponent;
    @ViewChild('dataTable') dataTable: Table;
    @ViewChild('paginator') paginator: Paginator;

    filterText = '';
    advancedFiltersAreShown = false;
    constructor(
        injector: Injector,
        private _supplierServiceProxy: SupplierServiceProxy,
        private _activatedRoute: ActivatedRoute,
        private _httpClient: HttpClient
    ) {
        super(injector)
    }

    createSupplier(): void {
        this.createOrEditSupplierModal.show();
    }

    getSuppliers(event?: LazyLoadEvent) {
        if (this.primengTableHelper.shouldResetPaging(event)) {
            this.paginator.changePage(0);

            return;
        }
        this.primengTableHelper.showLoadingIndicator();

        this._supplierServiceProxy
            .getSupplierList(
                this.filterText,
                this.primengTableHelper.getSorting(this.dataTable),
                this.primengTableHelper.getMaxResultCount(
                    this.paginator,
                    event
                ),
                this.primengTableHelper.getSkipCount(this.paginator, event)
            )
            .pipe(
                finalize(() => this.primengTableHelper.hideLoadingIndicator())
            )
            .subscribe(result => {
                this.primengTableHelper.totalRecordsCount = result.totalCount
                this.primengTableHelper.records = result.items
                this.primengTableHelper.hideLoadingIndicator()
            });
    }
    reloadPage(): void {
        this.paginator.changePage(this.paginator.getPage());
    }

    deleteSupplier(supplier: SupplierListDto): void {
        this.message.confirm(
            this.l('UserDeleteWarningMessage', supplier.displayName),
            this.l('AreYouSure'),
            isConfirmed => {
                if (isConfirmed) {
                    this._supplierServiceProxy
                        .deleteSupplier(supplier.id)
                        .subscribe(() => {
                            this.reloadPage();
                            this.notify.success(this.l('SuccessfullyDeleted'));
                        });
                }
            }
        )
    }
}
